//
//  CYFangchanVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/6/13.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYFangchanVC.h"
#import "LEUnitView+LE.h"
#import "CYCreditTool.h"
#import "CustomIOSAlertView.h"
#import "UIImageView+WebCache.h"
#import "MWPhotoBrowser.h"
#import "LEPhotoBrowserTool.h"
#import "CYLoginHttpTool.h"
#import "CYUser.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "LEImagePickerTool.h"
#import "MBProgressHUD+LE.h"
#import "CYDiscoveryHttpTool.h"
#import "UIButton+WebCache.h"

@interface CYFangchanVC ()
@property (weak, nonatomic) IBOutlet UIView *fangchanzmSubmitView;
@property (weak, nonatomic) IBOutlet UIImageView *shilituImage1View;
@property (weak, nonatomic) IBOutlet UIView *shouchisubmitView;
@property (weak, nonatomic) IBOutlet UIView *huanjingsubmitView;
@property (nonatomic, strong) NSMutableArray *fangchanzmUrls;
@property (nonatomic, strong) NSMutableArray *shouchiUrls;
@property (nonatomic, strong) NSMutableArray *huanjingUrls;
@property (nonatomic, strong) NSArray *fangchanzhengArr;
@property (nonatomic, strong) NSArray *shouchiArr;
@property (nonatomic, strong) NSArray *huanjingArr;

@property (nonatomic, strong) NSMutableDictionary *fangchanDic;
@property (weak, nonatomic) IBOutlet UILabel *fangchanzhengLab;
@property (weak, nonatomic) IBOutlet UILabel *shouchiLab;
@property (weak, nonatomic) IBOutlet UILabel *huanjingLab;
@property (weak, nonatomic) IBOutlet UIView *fangchanrecordView;
@property (weak, nonatomic) IBOutlet UILabel *fangchanrecordLab;
@property (nonatomic, strong) NSMutableArray *recordimageUrls;
@property (weak, nonatomic) IBOutlet UIButton *baipai1;
@property (weak, nonatomic) IBOutlet UIButton *baipai2;
@property (weak, nonatomic) IBOutlet UIButton *baipai3;
@property (weak, nonatomic) IBOutlet UIButton *baipai4;
@property (weak, nonatomic) IBOutlet UIButton *shouchi1;
@property (weak, nonatomic) IBOutlet UIButton *shouchi2;
@property (weak, nonatomic) IBOutlet UIButton *huanjing1;
@property (weak, nonatomic) IBOutlet UIButton *huanjing2;
@property (weak, nonatomic) IBOutlet UIButton *huanjing3;


@end

@implementation CYFangchanVC

- (void)viewDidLoad {
     [super viewDidLoad];
     self.title = @"房产";
     
     [_baipai1 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/1.png"] forState:UIControlStateNormal];
     [_baipai2 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/2.png"] forState:UIControlStateNormal];
     [_baipai3 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/3.png"] forState:UIControlStateNormal];
      [_baipai4 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/4.png"] forState:UIControlStateNormal];
     
     [_shouchi1 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/5.png"] forState:UIControlStateNormal];
     [_shouchi2 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/6.png"] forState:UIControlStateNormal];
     [_huanjing1 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/jiating/1.png"] forState:UIControlStateNormal];
     [_huanjing2 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/jiating/2.png"] forState:UIControlStateNormal];
     [_huanjing3 sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/jiating/3.png"] forState:UIControlStateNormal];
     

     
     _fangchanzmUrls = [[NSMutableArray alloc]init];
     _shouchiUrls = [[NSMutableArray alloc]init];
     _huanjingUrls = [[NSMutableArray alloc]init];
     _fangchanDic = [[NSMutableDictionary alloc]init];
     _recordimageUrls = [[NSMutableArray alloc]init];
     
     
     NSDictionary *fangchandic = [[NSDictionary alloc] initWithContentsOfFile:@"fangchanImage.plist"];
     _fangchanzhengArr = [fangchandic objectForKey:@"fangchanzmUrl"];
     _shouchiArr = [fangchandic objectForKey:@"shouchiUrl"];
     _huanjingArr = [fangchandic objectForKey:@"huanjingUrl"];
     
     if (_fangchanzhengArr.count) {
          _fangchanzhengLab.text = @"已上传，再次上传将会重新更新此类";
          _fangchanzhengLab.textColor = [UIColor redColor];
     }
     if (_shouchiArr.count) {
          _shouchiLab.text = @"已上传，再次上传将会重新更新此类";
          _shouchiLab.textColor = [UIColor redColor];
     }
     if (_huanjingArr.count) {
          _huanjingLab.text = @"已上传，再次上传将会重新更新此类";
          _huanjingLab.textColor = [UIColor redColor];
     }
     
     
     UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,50,30)];
     rightButton.titleLabel.textColor = [UIColor blackColor];
     [rightButton addTarget:self action:@selector(submitclick) forControlEvents:UIControlEventTouchUpInside];
     [rightButton setTitle:@"确定" forState:UIControlStateNormal];
     UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
     self.navigationItem.rightBarButtonItem= rightItem;

     [self setupUploadView];
     [self setimagerecordView];
    // Do any additional setup after loading the view from its nib.
}


-(void)setimagerecordView{
     // image 后缀
     NSString *preFixString = @"http://image.credit-manage.com/";
     // 屏幕的宽度 (也是承载imageView的view的宽度)
     CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
     // image各个边界的间隔
     CGFloat space = 10.f;
     // imageview的width (设定为5张)
     CGFloat imgWidth = (screenWidth - space * 6 ) / 5;
     // imageview的height
     CGFloat imgHeight = _fangchanrecordView.frame.size.height - space * 2;
     // 由于是xib + 手码, 代码获取xib控件的frame 必须在主线程中获取
     dispatch_async(dispatch_get_main_queue(), ^{
          CYUser *user = [CYLoginHttpTool userData].user;
          [CYHttpTool postWithURL:[CYURL shared].getAssetInfoPics params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
               //        CYLog(@"json = %@",json);
               CYJsonInfo *jsoninf = [CYJsonInfo objectWithKeyValues:json];
               NSMutableArray *data = jsoninf.data;
               for (NSDictionary *dic in data) {
                    // 取网络图片地址
                    NSString *assetPicAddress = [dic objectForKey:@"assetPicAddress"];
                    //取type
                    NSInteger type = [[dic objectForKey:@"type"] integerValue];
                    switch (type) {
                         case 1:
                              [_recordimageUrls addObject:assetPicAddress];
                              break;
                    }
               }
               for (int i = 0; i < _recordimageUrls.count; i++ ) {
                    // image的url string
                    NSString *imgUrlString = [_recordimageUrls objectAtIndex:i];
                    imgUrlString = [preFixString stringByAppendingString:imgUrlString];
                    // 创建imageView
                    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(space + (imgWidth + space) * i, space, imgWidth, imgHeight)];
                    imgView.backgroundColor = [UIColor whiteColor];
                    // 此处你可使用SDWebImage来加载
                    [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrlString]];
                    [_fangchanrecordView addSubview:imgView];
               }
               if (_recordimageUrls.count) {
                    _fangchanrecordLab.hidden = YES;
                    UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoBrowser:)];
                    [_fangchanrecordView addGestureRecognizer:tag];
               }
               
               
          } failure:^(NSError *error) {
               
          }];
     });
     
     
}

/**
 *  图片浏览器
 */
- (void)photoBrowser:(UITapGestureRecognizer *)tag {
     NSArray *urls = _recordimageUrls;
     MWPhoto *photo = nil;
     NSMutableArray *photos = [NSMutableArray array];
     for (NSString *url in urls) {
          photo = [MWPhoto photoWithURL:CYURLWithService(url)];
          //        photo.caption = @"title";
          [photos addObject:photo];
     }
     [LEPhotoBrowserTool showPhotoBrowserWithController:self photos:photos];
}




-(void)submitclick{
     
     [_fangchanDic removeAllObjects];
     if (_fangchanzmUrls.count) {
          [_fangchanDic setObject:_fangchanzmUrls forKey:@"fangchanzmUrl"];
          
     }else if (!_fangchanzmUrls.count && _fangchanzhengArr.count) {
          [_fangchanDic setObject:_fangchanzhengArr forKey:@"fangchanzmUrl"];

     }
     if (_shouchiUrls.count) {
          [_fangchanDic setObject:_shouchiUrls forKey:@"shouchiUrl"];
          
     }else if (!_shouchiUrls.count && _shouchiArr.count) {
          [_fangchanDic setObject:_shouchiArr forKey:@"shouchiUrl"];
          
     }
     if (_huanjingUrls.count) {
          [_fangchanDic setObject:_huanjingUrls forKey:@"huanjingUrl"];
          
     }
     else if (!_huanjingUrls.count && _huanjingArr.count) {
          [_fangchanDic setObject:_huanjingArr forKey:@"huanjingUrl"];
          
     }
     
     if (_fangchanzhengArr.count != 0) {
          
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *plistPath1= [paths objectAtIndex:0];
          NSLog(@"%@",plistPath1);
          //得到完整的路径名
          NSString *fileName = [plistPath1 stringByAppendingPathComponent:@"fangchanImage.plist"];
          //NSMutableDictionary *myDic = [[NSMutableDictionary alloc]init];
          //[myDic setValuesForKeysWithDictionary:cityDic];
          NSFileManager *fm = [NSFileManager defaultManager];
          
          if ([fm createFileAtPath:fileName contents:nil attributes:nil] ==YES) {
               [_fangchanDic writeToFile:fileName atomically:YES];
               NSLog(@"文件写入完成");
               CYLog(@"fangchandic = %@",_fangchanDic);
               [[NSNotificationCenter defaultCenter] postNotificationName:@"CYNewAssetNotification" object:nil userInfo:nil];
               [self.navigationController popViewControllerAnimated:YES];
               
          }
          return;
     }
     
     
     if (_fangchanzmUrls.count != 0) {
          
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *plistPath1= [paths objectAtIndex:0];
     NSLog(@"%@",plistPath1);
     //得到完整的路径名
     NSString *fileName = [plistPath1 stringByAppendingPathComponent:@"fangchanImage.plist"];
     //NSMutableDictionary *myDic = [[NSMutableDictionary alloc]init];
     //[myDic setValuesForKeysWithDictionary:cityDic];
     NSFileManager *fm = [NSFileManager defaultManager];
     
     if ([fm createFileAtPath:fileName contents:nil attributes:nil] ==YES) {
          [_fangchanDic writeToFile:fileName atomically:YES];
          NSLog(@"文件写入完成");
          CYLog(@"fangchandic = %@",_fangchanDic);
          [[NSNotificationCenter defaultCenter] postNotificationName:@"CYNewAssetNotification" object:nil userInfo:nil];
          [self.navigationController popViewControllerAnimated:YES];
          
        }
          
     }else{
          CYAlertView(@"请按照要求提交！");
          
     }
     
}


- (void)setupUploadView {
     //房产证
     [LEUnitView imageScrollView:self.fangchanzmSubmitView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
               [_fangchanzmUrls addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
     //手持
     [LEUnitView imageScrollView:self.shouchisubmitView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
               [_shouchiUrls addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
     
     //环境
     [LEUnitView imageScrollView:self.huanjingsubmitView controller:self block:^(LEUnitView *unitView, LEUnitCell *cell, UIImage *image) {
          [CYCreditTool uploadPicImage:image success:^(NSString *imageUrl) {
               [_huanjingUrls addObject:imageUrl];
               cell.ratio = 1;
          } failure:^(NSError *error) {
               //删除
               [unitView deleteLastCell];
          } uploadProgress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
               CGFloat a = totalBytesWritten;
               CGFloat b = totalBytesExpectedToWrite;
               cell.ratio = a/b - .01;
          }];
     } cellClick:^(LEUnitView *unitView, LEUnitCell *cell) {
     }];
}

- (IBAction)zhengmian1:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/1.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)zhengmian2:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/2.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)zhengmian3:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/3.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)zhengmian4:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/4.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)shouchi1:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/5.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)shouchi2:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/fangchan/6.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)shouchi3:(id)sender {
}
- (IBAction)shouchi4:(id)sender {
}
- (IBAction)huanjing1:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/jiating/1.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];

}
- (IBAction)huanjing2:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/jiating/2.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];
}
- (IBAction)huanjing3:(id)sender {
     UIImageView *shareView = [[UIImageView alloc] init];
     [shareView sd_setImageWithURL:[NSURL URLWithString:@"http://www.credit-manage.com:8058/apppic/jiating/3.png"]];
     //    CGFloat ratio = shareView.height / shareView.width;
     //    CGFloat height = CYScreeSize.height - 130;
     CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     CGFloat w = CYScreeSize.width - 32;
     shareView.frame = CGRectMake(0, 0, w, self.view.height-160);
     alertView.containerView = shareView;
     alertView.buttonTitles = @[@"知道了"];
     
     [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
     }];
     [alertView show];
}
- (IBAction)huanjing4:(id)sender {
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
